package webjoch.misc;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

import webjoch.Main;
import webjoch.methods.Links;
import webjoch.wrappers.Door;
import webjoch.wrappers.Explorer;
import webjoch.wrappers.Link;
import webjoch.wrappers.Room;

import com.rsbuddy.script.ActiveScript;
import com.rsbuddy.script.wrappers.Tile;
/**
 * Paint an overview of the explored dungeon map
 * @author Webjoch
 *
 */
public class PaintMap extends ActiveScript{
	public static ArrayList<Room> allRooms = new ArrayList<Room>();
	public static ArrayList<Boolean> roomDoors = new ArrayList<Boolean>();
	public static boolean highligh = false;
	
	public static Link[] lastPath = null;
	static Explorer path = null;
	
	public static void onRepaint(Graphics g1) {
		Graphics2D g = (Graphics2D) g1;
		
		int topX = 999999;
		int topY = 0;
		
		for (Link link : Main.getDungeon().getMap()){
			if (link.getRoom().getLocation().getX() < topX) topX = link.getRoom().getLocation().getX();
			if (link.getRoom().getLocation().getY() > topY) topY = link.getRoom().getLocation().getY();
			if (link.getLinkedRoom().getLocation().getX() < topX) topX = link.getLinkedRoom().getLocation().getX();
			if (link.getLinkedRoom().getLocation().getY() > topY) topY = link.getLinkedRoom().getLocation().getY();
		
			if (!allRooms.contains(link.getRoom())) allRooms.add(link.getRoom());
			if (!allRooms.contains(link.getLinkedRoom())) allRooms.add(link.getLinkedRoom());
		}
		
		g.setColor(new Color(0, 0, 0, highligh ? 200 : 50));
        g.fillRect(240, 15, 300, 250);
        g.setColor(new Color(255, 255, 255));
		
		for (Room room : allRooms){
			Tile location = room.getLocation();
			drawRoom(g, room, (int) Math.ceil((location.getX() - topX) * (double) 1.5 + 250), (int) Math.ceil((topY - location.getY()) * (double) 1.5 + 50));
		}
		if (path != null){
			lastPath = path.getPath();
			g.drawString("Path length: "+ lastPath.length, 240, 10);
			if (lastPath.length > 0){
				if (lastPath[lastPath.length-1].getRoom().inRoom()) path = null;
			}
		}
	}
	
	public static void setPath(Explorer path1){
		path = path1;
	}
	
	public static void drawRoom(Graphics2D g, Room room, int x, int y){
		try{
			if (lastPath != null){
				for (Link link : lastPath){
					if (link.getRoom() == null) continue;
					if (link.getRoom().getLocation().equals(room.getLocation())){
						if (link.getRotation() == Door.Rotation.EAST) g.drawLine(x + 10, y + 10, x + 35, y + 10);
						if (link.getRotation() == Door.Rotation.NORTH) g.drawLine(x + 10, y + 10, x + 10, y - 15);
						if (link.getRotation() == Door.Rotation.WEST) g.drawLine(x + 10, y + 10, x - 15, y + 10);
						if (link.getRotation() == Door.Rotation.SOUTH) g.drawLine(x + 10, y + 10, x + 10, y + 35);
						break;
					}
				}
			}
		}catch(Exception e){
			for (StackTraceElement info : e.getStackTrace()){
				if (info.getFileName() != null){
					System.out.println("File: "+ info.getFileName()+", line: "+info.getLineNumber());
				}
			}
		}
		
		boolean west = false;
		boolean north = false;
		boolean east = false;
		boolean south = false;
		
		for (Link link : Links.getRoomLinks(room)){
			if (link.getRotation() == Door.Rotation.WEST) west = true;
			if (link.getRotation() == Door.Rotation.NORTH) north = true;
			if (link.getRotation() == Door.Rotation.EAST) east = true;
			if (link.getRotation() == Door.Rotation.SOUTH) south = true;
		}
		
		if (west){
			g.drawLine(x, y, x, y+5);
			g.drawLine(x, y+15, x, y+20);
			g.drawLine(x, y+5, x-2, y+5);
			g.drawLine(x, y+15, x-2, y+15);
		}else{
			g.drawLine(x, y, x, y+20);
		}
		
		if (north){
			g.drawLine(x, y, x+5, y);
			g.drawLine(x+15, y, x+20, y);
			g.drawLine(x+5, y, x+5, y-2);
			g.drawLine(x+15, y, x+15, y-2);
		}else{
			g.drawLine(x, y, x+20, y);
		}
		
		if (east){
			g.drawLine(x+20, y, x+20, y+5);
			g.drawLine(x+20, y+15, x+20, y+20);
			g.drawLine(x+20, y+5, x+22, y+5);
			g.drawLine(x+20, y+15, x+22, y+15);
		}else{
			g.drawLine(x+20, y, x+20, y+20);
		}
		
		if (south){
			g.drawLine(x, y+20, x+5, y+20);
			g.drawLine(x+15, y+20, x+20, y+20);
			g.drawLine(x+5, y+20, x+5, y+22);
			g.drawLine(x+15, y+20, x+15, y+22);
		}else{
			g.drawLine(x, y+20, x+20, y+20);
		}
		
		if (room.inRoom()){
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		    Ellipse2D.Double hole = new Ellipse2D.Double();
		    hole.width = 5;
		    hole.height = 5;
		    hole.x = x + room.getLocalLocation(false)[0]; 
		    hole.y = y + 16 - room.getLocalLocation(false)[1];
		    g.draw(hole);
		}
	}
	
	@Override
	public int loop() {
		return 0;
	}
}
